let castle = {
    "castle_A1": "突比力斯堡",
    "castle_A2": "萨赫勒堡",
    "castle_A3": "艾因·巴力克堡",
    "castle_A4": "迈代尼堡",
    "castle_A5": "贾迈耶堡",
    "castle_A6": "希巴勒·祖姆尔堡",
    "castle_A7": "乌格巴堡",
    "castle_A8": "坦姆努堡",
    "castle_A9": "巴里哈勒堡",
    "castle_B1": "阿布·科梅尔堡",
    "castle_B2": "拉诺克·亨堡",
    "castle_B3": "德鲁伊莫尔堡",
    "castle_B4": "潘德拉克堡",
    "castle_B5": "雷姆托伊尔堡",
    "castle_B6": "弗林托格堡",
    "castle_B7": "阿斯特堡",
    "castle_B8": "乌瑟莱姆堡",
    "castle_EN1": "瓦拉戈斯堡",
    "castle_EN2": "罗卡那堡",
    "castle_EN3": "雷索斯堡",
    "castle_EN4": "伽俄斯堡",
    "castle_EN5": "阿特里翁堡",
    "castle_EN6": "阿塔科尼亚堡",
    "castle_EN7": "厄毗诺萨堡",
    "castle_EN8": "叙拉托斯堡",
    "castle_EN9": "墨卡罗维亚堡",
    "castle_ES1": "俄德律萨堡",
    "castle_ES2": "科雷尼亚堡",
    "castle_ES3": "墨利翁堡",
    "castle_ES4": "拉文尼亚堡",
    "castle_ES5": "摩雷尼亚堡",
    "castle_ES6": "塞斯塔代姆堡",
    "castle_ES7": "约格律斯堡",
    "castle_ES8": "卡诺普西斯堡",
    "castle_EW1": "加隆托堡",
    "castle_EW2": "托里俄斯堡",
    "castle_EW3": "俄尼卡堡",
    "castle_EW4": "色雷刻托堡",
    "castle_EW5": "维戎堡",
    "castle_EW6": "赫托该亚堡",
    "castle_EW7": "俄里斯托科律斯堡",
    "castle_EW8": "革耳塞戈斯堡",
    "castle_K1": "乌赛克堡",
    "castle_K2": "阿契赛尔堡",
    "castle_K3": "哈坤堡",
    "castle_K4": "泰佩斯堡",
    "castle_K5": "希木利堡",
    "castle_K6": "迪纳尔堡",
    "castle_K7": "西米拉堡",
    "castle_K8": "埃泽努尔堡",
    "castle_K9": "开撒尔堡",
    "castle_S1": "乌斯托科堡",
    "castle_S2": "马扎丹堡",
    "castle_S3": "涅维扬斯克堡",
    "castle_S4": "克拉尼罗格堡",
    "castle_S5": "奥夫堡",
    "castle_S6": "塔科尔堡",
    "castle_S7": "乌里克斯卡拉堡",
    "castle_S8": "弗拉基夫堡",
    "castle_V1": "于桑克堡",
    "castle_V2": "翁加尔堡",
    "castle_V3": "德拉庞堡",
    "castle_V4": "奥曼法德堡",
    "castle_V5": "蒂尔比堡",
    "castle_V6": "卡琉斯堡",
    "castle_V7": "塔利维尔堡",
    "castle_V8": "韦雷克桑堡",
}

let castleV = {
    "castle_village_A1_1": "突比力斯",
    "castle_village_A1_2": "法纳卜",
    "castle_village_A2_1": "萨赫勒",
    "castle_village_A2_2": "阿斯麦特",
    "castle_village_A3_1": "艾因·巴力克",
    "castle_village_A3_2": "代尔·哈瓦",
    "castle_village_A4_1": "迈代尼",
    "castle_village_A4_2": "吉德纳尔",
    "castle_village_A5_1": "贾迈耶",
    "castle_village_A5_2": "侯纳卜",
    "castle_village_A6_1": "希巴勒·祖姆尔",
    "castle_village_A6_2": "拉迈萨",
    "castle_village_A7_1": "乌格巴",
    "castle_village_A7_2": "本盖兹",
    "castle_village_A8_1": "坦姆努",
    "castle_village_A8_2": "库加",
    "castle_village_A9_1": "巴里哈勒",
    "castle_village_A9_2": "瓦达尔",
    "castle_village_B1_1": "阿布·科梅尔",
    "castle_village_B1_2": "因韦斯",
    "castle_village_B2_1": "拉诺克·亨",
    "castle_village_B2_2": "坎特雷克",
    "castle_village_B3_1": "德鲁伊莫尔",
    "castle_village_B3_2": "托·梅利纳",
    "castle_village_B4_1": "潘德拉克",
    "castle_village_B4_2": "林杜恩",
    "castle_village_B5_1": "雷姆托伊尔",
    "castle_village_B5_2": "克莱格·班",
    "castle_village_B6_1": "弗林托格",
    "castle_village_B6_2": "格林托尔",
    "castle_village_B7_1": "阿斯特",
    "castle_village_B7_2": "伊姆拉赫",
    "castle_village_B8_1": "乌瑟莱姆",
    "castle_village_B8_2": "肖尔达斯",
    "castle_village_EN1_1": "瓦拉戈斯",
    "castle_village_EN1_2": "艾俄里亚",
    "castle_village_EN2_1": "罗卡那",
    "castle_village_EN2_2": "诺耳塔尼萨",
    "castle_village_EN3_1": "雷索斯",
    "castle_village_EN3_2": "底俄帕利斯",
    "castle_village_EN4_1": "伽俄斯",
    "castle_village_EN4_2": "忒密斯",
    "castle_village_EN5_1": "阿特里翁",
    "castle_village_EN5_2": "马珊加拉",
    "castle_village_EN6_1": "阿塔科尼亚",
    "castle_village_EN6_2": "珀塔米斯",
    "castle_village_EN7_1": "厄毗诺萨",
    "castle_village_EN7_2": "庞斯",
    "castle_village_EN8_1": "叙拉托斯",
    "castle_village_EN8_2": "特美摩斯",
    "castle_village_EN9_1": "墨卡罗维亚",
    "castle_village_EN9_2": "阿加尔蒙",
    "castle_village_ES1_1": "俄德律萨",
    "castle_village_ES1_2": "恺拉",
    "castle_village_ES2_1": "科雷尼亚",
    "castle_village_ES2_2": "墨塔基亚",
    "castle_village_ES3_1": "墨利翁",
    "castle_village_ES3_2": "萨戈利那",
    "castle_village_ES4_1": "拉文尼亚",
    "castle_village_ES4_2": "厄忒弥萨",
    "castle_village_ES5_1": "摩雷尼亚",
    "castle_village_ES5_2": "阿特费尼亚",
    "castle_village_ES6_1": "塞斯塔代姆",
    "castle_village_ES6_2": "阿密孔",
    "castle_village_ES7_1": "约格律斯",
    "castle_village_ES7_2": "优纳利卡",
    "castle_village_ES8_1": "卡诺普西斯",
    "castle_village_ES8_2": "波普西亚",
    "castle_village_EW1_1": "加隆托",
    "castle_village_EW1_2": "吕西亚",
    "castle_village_EW2_1": "托里俄斯",
    "castle_village_EW2_2": "柏耳贡",
    "castle_village_EW3_1": "俄尼卡",
    "castle_village_EW3_2": "塔耳库提斯",
    "castle_village_EW4_1": "色雷刻托",
    "castle_village_EW4_2": "伽玛耳丹",
    "castle_village_EW5_1": "维戎",
    "castle_village_EW5_2": "戈勒林",
    "castle_village_EW6_1": "赫托该亚",
    "castle_village_EW6_2": "尼得翁",
    "castle_village_EW7_1": "俄里斯托科律斯",
    "castle_village_EW7_2": "厄尔凡尼亚",
    "castle_village_EW8_1": "革耳塞戈斯",
    "castle_village_EW8_2": "瓦忒亚",
    "castle_village_K1_1": "乌赛克",
    "castle_village_K1_2": "埃斯梅",
    "castle_village_K2_1": "阿契赛尔",
    "castle_village_K2_2": "喀木沙",
    "castle_village_K3_1": "哈坤",
    "castle_village_K3_2": "基拉兹",
    "castle_village_K4_1": "泰佩斯",
    "castle_village_K4_2": "库鲁卢克",
    "castle_village_K5_1": "希木利",
    "castle_village_K5_2": "鄂木罗托克",
    "castle_village_K6_1": "迪纳尔",
    "castle_village_K6_2": "喀拉哈力",
    "castle_village_K7_1": "西米拉",
    "castle_village_K7_2": "柯希·阿吉克",
    "castle_village_K8_1": "埃泽努尔",
    "castle_village_K8_2": "格烈登",
    "castle_village_K9_1": "开撒尔",
    "castle_village_K9_2": "帕亚木",
    "castle_village_S1_1": "乌斯托科",
    "castle_village_S1_2": "哲米扬",
    "castle_village_S2_1": "马扎丹",
    "castle_village_S2_2": "福林",
    "castle_village_S3_1": "涅夫扬斯克",
    "castle_village_S3_2": "德宁",
    "castle_village_S4_1": "克拉尼罗格",
    "castle_village_S4_2": "伊斯米尔科格",
    "castle_village_S5_1": "奥夫",
    "castle_village_S5_2": "费赫",
    "castle_village_S6_1": "塔科尔",
    "castle_village_S6_2": "德沃鲁斯塔",
    "castle_village_S7_1": "乌里克斯卡拉",
    "castle_village_S7_2": "阿洛夫",
    "castle_village_S8_1": "弗拉基夫",
    "castle_village_S8_2": "格拉夫斯特伦",
    "castle_village_V1_1": "于桑克",
    "castle_village_V2_1": "翁加尔",
    "castle_village_V2_2": "费顿",
    "castle_village_V3_1": "德拉庞",
    "castle_village_V3_2": "瓦朗比",
    "castle_village_V4_1": "奥曼法德",
    "castle_village_V5_1": "蒂尔比",
    "castle_village_V5_2": "西岚达克",
    "castle_village_V6_1": "卡琉斯",
    "castle_village_V6_2": "德里亚特",
    "castle_village_V7_1": "塔利维尔",
    "castle_village_V7_2": "罗德唐",
    "castle_village_V8_1": "韦雷克桑",
    "castle_village_V8_2": "马林",
}

let town = {
    "town_A1": "古亚兹",
    "town_A2": "侯森·富勒格",
    "town_A3": "以亚基斯",
    "town_A4": "拉齐赫",
    "town_A5": "胡比亚",
    "town_A6": "撒纳拉",
    "town_A7": "阿斯凯尔",
    "town_A8": "加西拉",
    "town_B1": "马鲁纳斯",
    "town_B2": "邓格拉尼斯",
    "town_B3": "卡·班塞斯",
    "town_B4": "肖农",
    "town_B5": "彭·坎诺克",
    "town_EN1": "厄庇克洛忒亚",
    "town_EN2": "狄亚特马",
    "town_EN3": "萨涅俄帕",
    "town_EN4": "阿耳戈隆",
    "town_EN5": "密泽亚",
    "town_EN6": "安普雷拉",
    "town_ES1": "达努斯提卡",
    "town_ES2": "沃斯特鲁姆",
    "town_ES3": "波罗斯",
    "town_ES4": "吕卡隆",
    "town_ES5": "俄尼拉",
    "town_ES6": "费卡翁",
    "town_ES7": "席隆尼亚",
    "town_EW1": "拉革塔",
    "town_EW2": "泽翁尼卡",
    "town_EW3": "贾尔马律斯",
    "town_EW4": "俄耳堤西亚",
    "town_EW5": "阿弥塔堤斯",
    "town_EW6": "洛泰",
    "town_K1": "巴尔塔罕",
    "town_K2": "阿克卡拉特",
    "town_K3": "马凯布",
    "town_K4": "奥通加德",
    "town_K5": "柴坎",
    "town_K6": "奥多赫",
    "town_S1": "瓦尔切格",
    "town_S2": "巴尔加德",
    "town_S3": "奥莫尔",
    "town_S4": "瓦尔诺瓦波尔",
    "town_S5": "蒂亚尔",
    "town_S6": "西比尔",
    "town_S7": "雷维尔",
    "town_V1": "萨哥特",
    "town_V2": "奥克斯·霍尔",
    "town_V3": "帕拉汶德",
    "town_V5": "加伦",
    "town_V6": "杰屈朗",
    "town_V7": "沙拉斯",
    "town_V8": "奥斯蒂港",
    "town_V9": "罗瓦尔",
}

let townV = {
    "village_A1_1": "塔舍巴",
    "village_A1_2": "巴格",
    "village_A1_4": "希卜莱特",
    "village_A2_2": "艾布·希",
    "village_A2_3": "胡加",
    "village_A3_1": "阿卜巴",
    "village_A3_3": "比尔·赛义夫",
    "village_A4_1": "加卜拉卜",
    "village_A4_2": "穆苏姆",
    "village_A4_4": "多加",
    "village_A5_1": "马赫卢勒",
    "village_A5_2": "利瓦斯",
    "village_A5_3": "瓦勒塔斯",
    "village_A6_1": "米贾伊特",
    "village_A6_2": "哈穆沙瓦",
    "village_A6_3": "贾哈西姆",
    "village_A6_4": "纳赫兰",
    "village_A7_2": "拜特·哈提夫",
    "village_A7_3": "马卜瓦兹",
    "village_A7_4": "扎勒姆",
    "village_A8_1": "艾兹贝特·纳胡勒",
    "village_A8_2": "阿卜甘",
    "village_B1_1": "达尔门格斯",
    "village_B1_2": "埃贝雷斯",
    "village_B1_3": "贝格洛米艾",
    "village_B1_4": "阿斯·卡瓦尔",
    "village_B2_1": "迪安托格麦尔",
    "village_B2_2": "格伦利斯里格",
    "village_B2_3": "莫里希格",
    "village_B3_1": "博格·贝斯",
    "village_B3_2": "格纳特·纳尔",
    "village_B3_3": "托·莱阿德",
    "village_B4_1": "布林·格拉斯",
    "village_B4_2": "安杜恩",
    "village_B4_3": "马格·阿尔巴",
    "village_B4_4": "斯温林",
    "village_B5_1": "杜恩",
    "village_B5_2": "盖恩塞斯",
    "village_B5_3": "韦农·埃蒂尔",
    "village_EN1_1": "马拉忒亚",
    "village_EN1_2": "斯塔堤摩斯",
    "village_EN1_3": "居摩斯",
    "village_EN2_1": "阿罗塞亚",
    "village_EN2_2": "耶拉科斯",
    "village_EN3_1": "厄诺伊萨",
    "village_EN3_3": "克里俄斯",
    "village_EN4_2": "阿拉堤斯",
    "village_EN4_4": "赫塔尼亚",
    "village_EN5_1": "威亚罗斯",
    "village_EN5_2": "俄耳特拉",
    "village_EN5_4": "萨马塔",
    "village_EN6_1": "阿瓦辛顿",
    "village_EN6_2": "玻瑞阿戈拉",
    "village_ES1_2": "波利西亚",
    "village_ES1_3": "泰格瑞索斯",
    "village_ES1_4": "厄瑞玻洛斯",
    "village_ES2_2": "戈耳科律斯",
    "village_ES2_3": "阿瓦吕普斯",
    "village_ES2_4": "阿利西翁",
    "village_ES3_1": "卡诺洛斯",
    "village_ES3_2": "特维亚",
    "village_ES3_3": "泽斯特亚",
    "village_ES4_1": "萨戈拉",
    "village_ES4_3": "坎忒里翁",
    "village_ES5_1": "兰塔斯",
    "village_ES5_2": "拉耳图绪斯",
    "village_ES5_3": "帕拉森诺斯",
    "village_ES6_1": "萨尔丹尼斯",
    "village_ES6_2": "斯珀提亚",
    "village_ES7_1": "索泰",
    "village_ES7_2": "瓦耳戈尼斯",
    "village_EW1_1": "普里墨索斯",
    "village_EW1_2": "蒙托斯",
    "village_EW2_2": "涅俄科律斯",
    "village_EW2_3": "阿尔萨索斯",
    "village_EW2_4": "泽俄科律斯",
    "village_EW3_2": "埃戈斯卡",
    "village_EW3_3": "得拉狄俄斯",
    "village_EW4_1": "阿耳波提斯",
    "village_EW4_3": "维尼拉",
    "village_EW4_4": "伽伦戈利亚",
    "village_EW5_1": "厄利帕",
    "village_EW5_2": "法索斯",
    "village_EW6_1": "卡耳斐尼翁",
    "village_EW6_3": "忒耳塞尼翁",
    "village_EW6_4": "勒布伦尼翁",
    "village_K1_1": "菲斯纳尔",
    "village_K1_2": "乌兰",
    "village_K1_4": "阿萨利格",
    "village_K2_1": "喀拉卡拉特",
    "village_K2_2": "帖斯密勒",
    "village_K3_1": "沙佩什特",
    "village_K3_2": "哈内希",
    "village_K3_3": "马津",
    "village_K4_2": "兰萨木",
    "village_K4_3": "米万占",
    "village_K4_4": "乌伦占",
    "village_K5_2": "奥胡坦",
    "village_K5_3": "依斯潘塔尔",
    "village_K5_4": "帕巴斯坦",
    "village_K6_1": "喀拉罕",
    "village_K6_2": "努丘克",
    "village_K6_3": "达纳拉",
    "village_S1_1": "罗多巴斯",
    "village_S1_3": "卡格雷夫",
    "village_S2_1": "萨夫纳",
    "village_S2_2": "马拉布罗特",
    "village_S3_1": "乔诺巴斯",
    "village_S3_2": "斯科林",
    "village_S4_1": "博乔瓦戈卡",
    "village_S4_3": "奥姆卡尼",
    "village_S4_4": "扬古图姆",
    "village_S5_1": "维西布罗特",
    "village_S5_2": "布基茨",
    "village_S6_1": "克沃尔",
    "village_S6_2": "拉达克梅德",
    "village_S6_3": "阿莱巴特",
    "village_S7_1": "科尔夏斯",
    "village_S7_2": "卡布尔",
    "village_V1_1": "卡利奥克",
    "village_V1_2": "埃蒂尔菲德",
    "village_V2_1": "马雷汶",
    "village_V2_2": "奥里唐",
    "village_V2_3": "弗雷吉昂",
    "village_V3_2": "鲁兰德",
    "village_V3_3": "拉尔纳克",
    "village_V3_4": "帕利松",
    "village_V5_1": "菲尔贝克",
    "village_V5_2": "梅罗克",
    "village_V5_3": "诺格伦",
    "village_V6_1": "阿罗曼克",
    "village_V6_2": "莫特",
    "village_V6_3": "阿洛斯唐",
    "village_V6_4": "绍纳尔",
    "village_V7_1": "萨万特",
    "village_V7_2": "韦桑",
    "village_V8_1": "奥尔斯热",
    "village_V8_2": "卡南克",
    "village_V8_3": "勒芒塔尔",
    "village_V9_1": "阿兰塔斯",
    "village_V9_2": "阿利斯维斯特",
}

let villages = {...castleV, ...townV}
let bounds = { ...town}

let fs = require('fs')

let strs = fs.readFileSync("./tmp").toString()

let Settlement = strs.match(/<Settlement(?:(?!Settlement>)[\w\W])+<\/Settlement>/g)

let village_typeCn = {
    'silk_plant':"生丝",            'cattle_farm':"牛场",
    'silver_mine':"银矿",           'iron_mine':"铁矿",
    'lumberjack':"硬木",            'wheat_farm':"麦田",
    'fisherman':"鱼",             'europe_horse_ranch':"帝国马牧场",
    'sheep_farm':"羊",            'flax_plant':"亚麻",
    'vineyard':"葡萄",              'date_farm':"枣",
    'olive_trees':"橄榄",           'swine_farm':"猪",
    'salt_mine':"盐",             'clay_mine':"黏土",
    'sturgian_horse_ranch':"斯特马",  'trapper':"毛皮",
    'desert_horse_ranch':"沙漠马",    'vlandian_horse_ranch':"瓦兰马",
    'battanian_horse_ranch':"巴丹马", 'steppe_horse_ranch':"草原马", 
}
let village_types = []
let castle_village = []
let towns = {}
Settlement.forEach( item => {
    let tmp = {}
    if(/posX="([^"]+)" posY="([^"]+)"/.test(item)) {
        tmp.x = RegExp.$1;
        tmp.y = RegExp.$2;
    }
    if(/<Village id="([^"]+)"/.test(item)) {
        let vid = /<Settlement id="([^"]+)"/.exec(item)[1]
        tmp.name = villages[vid]
        if(/village_type="([^"]+)"/.test(item)){
            tmp.village_type=village_typeCn[RegExp.$1.split(".")[1]];
            if(!village_types.includes(tmp.village_type)){
                village_types.push(tmp.village_type)
            }
        }
        if(/hearth="([^"]+)"/.test(item)){
            tmp["人口"]=RegExp.$1;
        }
        if(!/castle_village/.test(vid) && /trade_bound="([^"]+)"/.test(item)){
            let boundId = RegExp.$1.split(".")[1]
            if(bounds[boundId].name) {
                bounds[boundId].v.push(tmp)
            } else {
                bounds[boundId] = {
                    name: bounds[boundId],
                    v: [tmp]
                }
            }
        } else if (/castle_village/.test(vid)) {
            castle_village.push(tmp)
        }
    }
    if(/<Town id="[^"]+" is_castle="false"/.test(item)) {
        let vid = /<Settlement id="([^"]+)"/.exec(item)[1]
        towns[vid] = tmp
    }
})

for (const key in bounds) {
    bounds[key].x = towns[key].x
    bounds[key].y = towns[key].y
    bounds[key].castle_v = []
}




let ret = []
for (const key in bounds) {
    let data = bounds[key]
    ret.push(data)
}

castle_village.forEach(cv => {
    ret.sort((a,b) => {
        return Math.sqrt((a.x - cv.x)**2 + (a.y - cv.y)**2) - Math.sqrt((b.x - cv.x)**2 + (b.y - cv.y)**2)
    })[0].castle_v.push(cv)
})

ret.sort((a,b) => {
    return getpoint(b) - getpoint(a)
})


function getpoint(item) {
    let point = 0
    item.v.forEach(v => { // 人口
        point += 18
        if(["鱼","橄榄","枣","葡萄"].includes(v.village_type)) {
            point += 2.475
        } else if (v.village_type == "麦田") {
            point += 4.95
        } else {
            point += 1.65
        }
    })
    item.castle_v.forEach(v => {
        if(["鱼","橄榄","枣","葡萄"].includes(v.village_type)) {
            point += 2.475
        } else if (v.village_type == "麦田") {
            point += 4.95
        } else {
            point += 1.65
        }
    })
    return point
}
ret = ret.map(item => {
    let {name, v, castle_v} = item
    // "name": "革耳塞戈斯",
        // "village_type": "银矿",
        // "人口": "201"
    return `${name}  附属村庄:${v.map(v1 => `${v1.name}(${v1.village_type})${v1.人口}`).join(" ; ")}\n附近城堡村庄:${castle_v.map(v1 => `${v1.name}(${v1.village_type})${v1.人口}`).join(" ; ")}`
})
fs.writeFileSync("./tmp.md", ret.join("\n\n"))
{/* <Village id="castle_village_comp_EN1_1" village_type="VillageType.silk_plant" hearth="322" gate_rotation="0.008" trade_bound="Settlement.castle_EN1" bound="Settlement.castle_EN1" background_crop_position="0.0" background_mesh="gui_bg_village_empire" wait_mesh="wait_empire_village" castle_background_mesh="gui_bg_castle_empire" /> */}

